﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using NearForums.Configuration;

namespace NearForums.DataAccess.Xml
{
    class PageContentsDataAccess : BaseDataAccess, IPageContentsDataAccess
    {
        public void Add(PageContent content)
        {
            string filePath = GetDataPath(PagesFile);
            XDocument doc = XDocument.Load(filePath);
            int count = (int)doc.Root.Attribute("Count");
            int pageId = ++count;
            doc.Root.SetAttributeValue("Count", count);
            XElement elem = new XElement("Page",
                                new XAttribute("Id", pageId),
                                new XAttribute("ShortName", content.ShortName),
                                new XAttribute("Title", content.Title),
                                new XAttribute("EditDate", ToString(DateTime.Now)));
            doc.Root.AddFirst(elem);
            doc.Save(filePath);

            string filePath2 = GetDataPath(string.Format(PageFile, content.ShortName));
            XDocument doc2 = XDocument.Parse("<Page></Page>");
            doc2.Root.SetAttributeValue("Id", pageId);
            doc2.Root.SetAttributeValue("ShortName", content.ShortName);
            doc2.Root.SetAttributeValue("Title", content.Title);
            doc2.Root.SetValue(SiteConfiguration.HtmlEncoder(content.Body));
            doc2.Save(filePath2);
        }

        public bool Delete(string name)
        {
            string pageFilePath = GetDataPath(string.Format(PageFile, name));
            if (File.Exists(pageFilePath))
            {
                File.Delete(pageFilePath);
            }

            string filePath = GetDataPath(PagesFile);
            XDocument doc = XDocument.Load(filePath);
            var qry = from p in doc.Root.Elements("Page")
                      where (string)p.Attribute("ShortName") == name
                      select p;

            var pageElem = qry.SingleOrDefault();
            if (pageElem != null)
            {
                pageElem.Remove();
                doc.Save(filePath);
                return true;
            }

            return false;
        }

        public void Edit(PageContent content)
        {
            // shortname is the id
            // set the title
            string filePath = GetDataPath(PagesFile);
            XDocument doc = XDocument.Load(filePath);
            var qry = from p in doc.Root.Elements("Page")
                      where (string)p.Attribute("ShortName") == content.ShortName
                      select p;

            var pageElem = qry.SingleOrDefault();
            if (pageElem != null)
            {
                pageElem.SetAttributeValue("Title", content.Title);
                doc.Save(filePath);
            }

            string pageFilePath = GetDataPath(string.Format(PageFile, content.ShortName));
            XDocument pageDoc = XDocument.Load(pageFilePath);
            pageDoc.Root.SetAttributeValue("Title", content.Title);
            pageDoc.Root.SetValue(SiteConfiguration.HtmlEncoder(content.Body));
            pageDoc.Save(pageFilePath);
        }

        public PageContent Get(string name) {
            PageContent page = new PageContent();
            page.ShortName = name;

            string filePath = GetDataPath(string.Format(PageFile, name));
            XDocument doc = XDocument.Load(filePath);
            page.Title = (string)doc.Root.Attribute("Title");
            page.Body = SiteConfiguration.HtmlDecoder((string)doc.Root);
            return page;
        }

        public List<PageContent> GetAll()
        {
            XDocument doc = XDocument.Load(GetDataPath(PagesFile));

            List<PageContent> pages = new List<PageContent>();
            foreach (var elem in doc.Root.Elements("Page"))
            {
                string name = (string)elem.Attribute("ShortName");
                pages.Add(Get(name));
            }
            return pages;
        }

        public string GetAvailableShortName(string shortName)
        {
            string name = shortName;
            string filePath = GetDataPath(string.Format(PageFile, name));
            int index = 0;
           
            while (File.Exists(filePath))
            {
                index++;
                name = shortName + "-" +  index.ToString();
                filePath = GetDataPath(string.Format(PageFile, name));
            }

            return name;
        }
    }
}
